﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using btom = Microsoft.BizTalk.ExplorerOM;

namespace HenIT.BizTalk
{
    public class TrackingOptions
    {
        #region availableTrackingTypes
        private btom.TrackingTypes[] availableReceiveTrackingTypes = new Microsoft.BizTalk.ExplorerOM.TrackingTypes[]
        {
            btom.TrackingTypes.BeforeReceivePipeline,
            btom.TrackingTypes.AfterReceivePipeline,
            btom.TrackingTypes.BeforeSendPipeline,
            btom.TrackingTypes.AfterSendPipeline,
            btom.TrackingTypes.TrackPropertiesBeforeReceivePipeline,
            btom.TrackingTypes.TrackPropertiesAfterReceivePipeline,
            btom.TrackingTypes.TrackPropertiesBeforeSendPipeline,
            btom.TrackingTypes.TrackPropertiesAfterSendPipeline
        };
        private btom.TrackingTypes[] availableSendTrackingTypes = new Microsoft.BizTalk.ExplorerOM.TrackingTypes[]
        {
            btom.TrackingTypes.BeforeReceivePipeline,
            btom.TrackingTypes.AfterReceivePipeline,
            btom.TrackingTypes.BeforeSendPipeline,
            btom.TrackingTypes.AfterSendPipeline,
            btom.TrackingTypes.TrackPropertiesBeforeReceivePipeline,
            btom.TrackingTypes.TrackPropertiesAfterReceivePipeline,
            btom.TrackingTypes.TrackPropertiesBeforeSendPipeline,
            btom.TrackingTypes.TrackPropertiesAfterSendPipeline
        };
        private btom.OrchestrationTrackingTypes[] availableOrchestrationTrackingTypes = new Microsoft.BizTalk.ExplorerOM.OrchestrationTrackingTypes[]
        {
            btom.OrchestrationTrackingTypes.ServiceStartEnd,
            btom.OrchestrationTrackingTypes.MessageSendReceive,
            btom.OrchestrationTrackingTypes.InboundMessageBody,
            btom.OrchestrationTrackingTypes.OutboundMessageBody,
            btom.OrchestrationTrackingTypes.OrchestrationEvents,
            btom.OrchestrationTrackingTypes.TrackPropertiesForIncomingMessages,
            btom.OrchestrationTrackingTypes.TrackPropertiesForOutgoingMessages
        };
        private btom.PipelineTrackingTypes[] availablePipelineTrackingTypes = new Microsoft.BizTalk.ExplorerOM.PipelineTrackingTypes[]
        {
                btom.PipelineTrackingTypes.ServiceStartEnd,
                btom.PipelineTrackingTypes.MessageSendReceive,
                btom.PipelineTrackingTypes.InboundMessageBody,
                btom.PipelineTrackingTypes.OutboundMessageBody
        }; 
        #endregion
        
        private List<Application> applications = new List<Application>();
        public List<Application> Applications
        {
            get { return applications; }
        }

        public TrackingOptions(string trackingOptionsFilePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(trackingOptionsFilePath);
            foreach (XmlNode appNode in doc.SelectNodes("trackingOptions/application"))
            {
                applications.Add(ReadApplication(appNode));
            } 
        }

        #region ReadSections
        private Application ReadApplication(XmlNode appNode)
        {
            Application application = new Application();
            application.Name = appNode.Attributes.GetNamedItem("name").Value;

            foreach (XmlNode receivePortNode in appNode.SelectNodes("receivePorts/receivePort"))
            {
                application.ReceivePorts.Add(ReadReceivePort(receivePortNode));
            }
            foreach (XmlNode sendPortNode in appNode.SelectNodes("sendPorts/sendPort"))
            {
                application.SendPorts.Add(ReadSendPort(sendPortNode));
            }
            foreach (XmlNode orchestrationNode in appNode.SelectNodes("orchestrations/orchestration"))
            {
                application.Orchestrations.Add(ReadOrchestration(orchestrationNode));
            }
            foreach (XmlNode pipelineNode in appNode.SelectNodes("pipelines/pipeline"))
            {
                application.Pipelines.Add(ReadPipeline(pipelineNode));
            }
            return application;
        }
        private ReceivePort ReadReceivePort(XmlNode receivePortNode)
        {
            ReceivePort receivePort = new ReceivePort();
            receivePort.Name = receivePortNode.Attributes.GetNamedItem("name").Value;
            if (receivePortNode.SelectSingleNode("tracking/add[@key='*']") != null)
            {
                foreach (btom.TrackingTypes trackingType in availableReceiveTrackingTypes)
                {
                    receivePort.AddOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode addNode in receivePortNode.SelectNodes("tracking/add"))
                {
                    foreach (btom.TrackingTypes trackingType in availableReceiveTrackingTypes)
                    {
                        AddTrackingTypes(receivePort.AddOptions, addNode, trackingType);
                    }
                }
            }

            if (receivePortNode.SelectSingleNode("tracking/remove[@key='*']") != null)
            {
                foreach (btom.TrackingTypes trackingType in availableReceiveTrackingTypes)
                {
                    receivePort.RemoveOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode removeNode in receivePortNode.SelectNodes("tracking/remove"))
                {
                    foreach (btom.TrackingTypes trackingType in availableReceiveTrackingTypes)
                    {
                        AddTrackingTypes(receivePort.RemoveOptions, removeNode, trackingType);
                    }
                }
            }

            return receivePort;
        }
        private SendPort ReadSendPort(XmlNode sendPortNode)
        {
            SendPort sendPort = new SendPort();
            sendPort.Name = sendPortNode.Attributes.GetNamedItem("name").Value;
            if (sendPortNode.SelectSingleNode("tracking/add[@key='*']") != null)
            {
                foreach (btom.TrackingTypes trackingType in availableSendTrackingTypes)
                {
                    sendPort.AddOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode addNode in sendPortNode.SelectNodes("tracking/add"))
                {
                    foreach (btom.TrackingTypes trackingType in availableSendTrackingTypes)
                    {
                        AddTrackingTypes(sendPort.AddOptions, addNode, trackingType);
                    }
                }
            }
            if (sendPortNode.SelectSingleNode("tracking/remove[@key='*']") != null)
            {
                foreach (btom.TrackingTypes trackingType in availableSendTrackingTypes)
                {
                    sendPort.RemoveOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode removeNode in sendPortNode.SelectNodes("tracking/remove"))
                {
                    foreach (btom.TrackingTypes trackingType in availableSendTrackingTypes)
                    {
                        AddTrackingTypes(sendPort.RemoveOptions, removeNode, trackingType);
                    }
                }
            }
            return sendPort;
        }
        private Orchestration ReadOrchestration(XmlNode orchestrationNode)
        {
            Orchestration orchestration = new Orchestration();
            orchestration.Name = orchestrationNode.Attributes.GetNamedItem("name").Value;
            if (orchestrationNode.SelectSingleNode("tracking/add[@key='*']") != null)
            {
                foreach (btom.OrchestrationTrackingTypes trackingType in availableOrchestrationTrackingTypes)
                {
                    orchestration.AddOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode addNode in orchestrationNode.SelectNodes("tracking/add"))
                {
                    foreach (btom.OrchestrationTrackingTypes trackingType in availableOrchestrationTrackingTypes)
                    {
                        AddTrackingTypes(orchestration.AddOptions, addNode, trackingType);
                    }
                }
            }
            if (orchestrationNode.SelectSingleNode("tracking/remove[@key='*']") != null)
            {
                foreach (btom.OrchestrationTrackingTypes trackingType in availableOrchestrationTrackingTypes)
                {
                    orchestration.RemoveOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode removeNode in orchestrationNode.SelectNodes("tracking/remove"))
                {
                    foreach (btom.OrchestrationTrackingTypes trackingType in availableOrchestrationTrackingTypes)
                    {
                        AddTrackingTypes(orchestration.RemoveOptions, removeNode, trackingType);
                    }
                }
            }
            return orchestration;
        }
        private Pipeline ReadPipeline(XmlNode pipelineNode)
        {
            Pipeline pipeline = new Pipeline();
            pipeline.Name = pipelineNode.Attributes.GetNamedItem("name").Value;
            if (pipelineNode.SelectSingleNode("tracking/add[@key='*']") != null)
            {
                foreach (btom.PipelineTrackingTypes trackingType in availablePipelineTrackingTypes)
                {
                    pipeline.AddOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode addNode in pipelineNode.SelectNodes("tracking/add"))
                {
                    foreach (btom.PipelineTrackingTypes trackingType in availablePipelineTrackingTypes)
                    {
                        AddTrackingTypes(pipeline.AddOptions, addNode, trackingType);
                    }
                }
            }
            if (pipelineNode.SelectSingleNode("tracking/remove[@key='*']") != null)
            {
                foreach (btom.PipelineTrackingTypes trackingType in availablePipelineTrackingTypes)
                {
                    pipeline.RemoveOptions.Add(trackingType);
                }
            }
            else
            {
                foreach (XmlNode removeNode in pipelineNode.SelectNodes("tracking/remove"))
                {
                    foreach (btom.PipelineTrackingTypes trackingType in availablePipelineTrackingTypes)
                    {
                        AddTrackingTypes(pipeline.RemoveOptions, removeNode, trackingType);
                    }
                }
            }
            return pipeline;
        } 
        #endregion

        #region AddTrackingTypes
        private void AddTrackingTypes(List<btom.TrackingTypes> list, XmlNode addNode, btom.TrackingTypes trackingType)
        {
            if (addNode.Attributes.GetNamedItem("key") != null)
            {
                if (addNode.Attributes.GetNamedItem("key").Value.ToLower() == trackingType.ToString().ToLower())
                    list.Add(trackingType);
            }
        }
        private void AddTrackingTypes(List<btom.OrchestrationTrackingTypes> list, XmlNode addNode, btom.OrchestrationTrackingTypes orchestrationTrackingType)
        {
            if (addNode.Attributes.GetNamedItem("key") != null)
            {
                if (addNode.Attributes.GetNamedItem("key").Value.ToLower() == orchestrationTrackingType.ToString().ToLower())
                    list.Add(orchestrationTrackingType);
            }
        }
        private void AddTrackingTypes(List<Microsoft.BizTalk.ExplorerOM.PipelineTrackingTypes> list, XmlNode addNode, Microsoft.BizTalk.ExplorerOM.PipelineTrackingTypes pipelineTrackingType)
        {
            if (addNode.Attributes.GetNamedItem("key") != null)
            {
                if (addNode.Attributes.GetNamedItem("key").Value.ToLower() == pipelineTrackingType.ToString().ToLower())
                    list.Add(pipelineTrackingType);
            }
        } 
        #endregion

    }
}